/**
 * @desc 用户对应处理层
 */

import { ErrLogger } from '../middleware/logger';
import ErrorCode, { SuccessCode } from '../utils/errorCode';
const SystemModel = require('../db/system');
const UserModel = require('../db/user');

// 异常code
const {
  Error500,
} = ErrorCode;

/**
 * 获取首页信息
 */
export const CGetIndexInfo = () => {
  return new Promise(async (resolve, reject) => {
    try {
      // 获取用户信息
      const vUserList = await UserModel.find();
      let partake = 0, visit = 0, ticket = 0;
      if (vUserList && vUserList.length > 0) {
        partake = vUserList.length;
        vUserList.forEach((item: any) => {
          ticket += item.ticket;
        });
        visit = partake * 2 + ticket + 20;
      }
      // 获取系统信息
      const result = await SystemModel.find();
      resolve({
        ...SuccessCode,
        obj: {
          partake,
          ticket,
          visit,
          activitytime: result[0].activitytime
        }
      });
    } catch (err) {
      ErrLogger({
        name: '获取首页信息出错',
        err,
      });
      reject(Error500);
    }
  });
}
